package com.daytask.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.daytask.common.ControllorCommon;
import com.daytask.formbean.SonTasktype;
import com.daytask.formbean.TasktypeFrom;
import com.daytask.util.ControllorUtils;
import com.daytask.validate.SonTasktypeVal;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

public class SonTasktypeController extends Controller {
	// id name caption daynum  weeknum monthnum yearnum  tasktypeid tasktypecaption
	@SuppressWarnings("rawtypes")
	public Map<String, Object> responseM = new HashMap<String, Object>();// 用来存放返回的数据（json）

	@Before(SonTasktypeVal.class)
	public void add() {
		boolean saveResult = false;
		SonTasktype form = getModel(SonTasktype.class);
		if (StringUtils.isNotBlank(form.getStr("name"))  
				&& StringUtils.isNotBlank(form.getStr("caption")) && form.getInt("tasktypeid")>0  ) {
			TasktypeFrom tasktype = TasktypeFrom.dao.findById(form.getInt("tasktypeid"));
			if (tasktype != null) {
				form.set("tasktypeid", tasktype.get("id"));
				form.set("tasktypecaption", tasktype.get("caption"));
				form.set("userid", ControllorUtils.getUserid());
				saveResult = form.save();
			}
		}
		if (saveResult == true) {
			ControllorUtils.createJsonResultMap(responseM, 1);
		} else {
			ControllorUtils.createJsonResultMap(responseM, 0);
		}
		renderJson(responseM);
	}

	public void delete() {
		String deleteIds = getPara("deleteIds");
		int deleteRecord = 0;
		if (StringUtils.isNotBlank(deleteIds)) {
			String ids[] = StringUtils.split(deleteIds, ",");
			for (String id : ids) {
					if (SonTasktype.dao.deleteById(id) == true)
						deleteRecord++;
			}
			if (deleteRecord == ids.length) {
				ControllorUtils.createJsonResultMap(responseM, 1);
			} else if (deleteRecord > 0 && deleteRecord < ids.length) {
				ControllorUtils.createJsonResultMap(responseM, 2);// 部分删除成功
			} else {
				ControllorUtils.createJsonResultMap(responseM, 0); // 删除失败
			}
		} else {
			ControllorUtils.createJsonResultMap(responseM, 0); // 删除失败

		}
		renderJson(responseM);
	}

	public void list() {
	//	String sql = "select a.*,b.name  ";
		int page =Integer.parseInt( getPara("page"));
		int rows = Integer.parseInt(getPara("rows"));
		page=page<1?1:page;
		String select  ="select  a.*,b.name tasktypeName";
		String fromSql ="from tb_son_tasktype a , tb_tasktype b where a.tasktypeid= b.id ";
		Page<TasktypeFrom> list = TasktypeFrom.dao.paginate(page,rows, select , fromSql);
		 responseM.put("total", list.getTotalRow());
		 responseM.put("rows",list.getList());
		  renderJson(responseM);
	 
	}

	public void edit() {
		boolean result = false;
		SonTasktype form = getModel(SonTasktype.class);
		int id = form.getInt("id");
		if (StringUtils.isNotBlank(form.getStr("name"))  
				&& StringUtils.isNotBlank(form.getStr("caption")) && form.getInt("tasktypeid")>0) {
			SonTasktype dbObj = form.findById(id);
			if (dbObj != null) {
				dbObj.set("name", form.getStr("name"));
				dbObj.set("caption", form.getStr("caption"));
				dbObj.set("tasktypeid",form.getInt("tasktypeid"));
				dbObj.set("daynum",form.getDouble("daynum"));
				dbObj.set("weeknum",form.getDouble("weeknum"));
				dbObj.set("monthnum"	,form.getDouble("monthnum"));
				dbObj.set("yearnum",form.getDouble("yearnum"));
				result = dbObj.update( );
			}
		}
		if (result == true) {
			ControllorUtils.createJsonResultMap(responseM, 1);
		} else {
			ControllorUtils.createJsonResultMap(responseM, 0);
		}
		renderJson(responseM);
	}
	
	public void selection() {
		
		List<SonTasktype> list = SonTasktype.dao.find("select * from tb_son_tasktype ");
		renderJson(list);
		
	}
	
	
	public void leavel(){
		renderJson(ControllorCommon.getLevels());
		
		
	}

}
